package com.zhao.base;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zhao.model.AccessToken;
import com.zhao.model.Ticket;

/**
 * 获取AccessToken
 * @author zhaowj
 * @date2018年1月20日 下午10:54:03
 */
public class TokenThread implements Runnable{
	 
	  private static Logger log = LoggerFactory.getLogger(TokenThread.class);  
	    // 第三方用户唯一凭证  
	    public static String appid = "";  
	    // 第三方用户唯一凭证密钥  
	    public static String appsecret = "";  
	    //商户号
	    public static String mch_id = "";  
	    
	    public static String api_key="";
	    
	    public static AccessToken accessToken =null;
	    
	    //调用js接口凭证
	    public static Ticket ticket=null;
	  
	    public void run() {  
	    	/*String[] signArr=new String[] {"appId","appsecret","token"};
			Map<String,String> signMap=CommonUtil.readValue("/wx.properties",signArr);
			appid=signMap.get("appId");
			appsecret=signMap.get("appsecret");*/
	        while (true) {  
	            try {  
	            	accessToken =AccessTokenBase.getAccessToken(appid, appsecret);
	            	ticket=TicketBase.getTicket(accessToken.getAccessToken());
	                if (null != accessToken) {  
	                    log.info("获取access_token成功，有效时长{}秒 token:{}", accessToken.getExpiresIn(), accessToken.getAccessToken());  
	                    log.info("获取ticket成功，有效时长{}秒 token:{}", ticket.getExpiresIn(), ticket.getTicket());  
	                    // 休眠7000秒  
	                    Thread.sleep((accessToken.getExpiresIn()- 200) * 1000);  
	                } else {  
	                    // 如果access_token为null，60秒后再获取  
	                    Thread.sleep(60 * 1000);  
	                }  
	            } catch (InterruptedException e) {  
	                try {  
	                    Thread.sleep(60 * 1000);  
	                } catch (InterruptedException e1) {  
	                    log.error("{}", e1);  
	                }  
	                log.error("{}", e);  
	            }  
	        }  
	    }  
}
